package com.monitor.module.sediment.controller;

import com.monitor.entity.pojo.user.User;
import com.monitor.entity.request.sediment.SedimentFindRequest;
import com.monitor.entity.response.Rs;
import com.monitor.module.sediment.service.SedimentService;
import com.monitor.utils.LicensePlate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 摄像头传输渣土车数据.
 *
 * @author Li Zhiyuan
 * @since 2020-11-18
 */
@RestController
@Slf4j
@RequestMapping("/sediment")
@Api(tags = "摄像头传输渣土车数据")
public class SedimentController {
    @Autowired
    private SedimentService service;
    @Resource
    private LicensePlate lp;

    @Resource
    private RedisTemplate<String, User> redisTemplate;

    @PostMapping("/upload")
    @ApiOperation(value = "传入渣土车数据", notes = "mac:摄像头唯一标识,frontPhoto:车头照片,bodyPhoto:车身照片")
    public ResponseEntity<Rs<Object>> upload(HttpServletRequest request) {
        //"mac:摄像头唯一标识,licensePlate:车牌号码,frontPhoto:车头照片,bodyPhoto:车身照片"
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        String mac = request.getParameter("mac");
        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
        //车头照片
        MultipartFile frontPhoto = fileMap.get("frontPhoto");
        //接百度车牌识别接口每天200次免费
        String licensePlate = lp.licensePlate(frontPhoto).getWords_result().getNumber();

        MultipartFile bodyPhoto = fileMap.get("bodyPhoto");
        if (mac == null) {
            return Rs.fail(Rs.map().put("mac", "摄像头唯一标识不能为null"));
        } else if (frontPhoto == null) {
            return Rs.fail(Rs.map().put("frontPhoto", "车头照片不能为null"));
        } else if (bodyPhoto == null) {
            return Rs.fail(Rs.map().put("bodyPhoto", "车身照片不能为null"));
        }

        return service.upload(mac, licensePlate, frontPhoto, bodyPhoto);
    }


    /**
     * 分页查询每页5条碴土车信息
     */
    @GetMapping
    @ApiOperation(value = "分页查询渣土车信息", notes = "分页查询渣土车信息可传查询参数")
    public ResponseEntity<Rs<Object>> page(SedimentFindRequest sedimentFindRequest,HttpServletRequest request) {
//        r.getSession().getAttribute("user");
        sedimentFindRequest.setCompanyId(redisTemplate.opsForValue().get(request.getHeader("token")).getCompanyId());
        return Rs.ok(Rs.map().put("data", service.findPage(sedimentFindRequest)));
    }


}
